/*
*   DebugHUD.h
*   ___PROJECTNAME___
*
*   Created by ___FULLUSERNAME___ on ___DATE___.
*   Copyright ___ORGANIZATIONNAME___ ___YEAR___. All rights reserved.
*
*/


#include "DebugHUD.h"

#include "Defines.h"
#include "Keys.h"
#include "Config.h"

#if (USE_GLES_RENDERER == 1)
#include "CEGLESRenderNode.h"
#endif

#include "CEResetedSliderModule.h"
#include "CESliderGroup.h"

#include "Box2D.h"


using namespace cocos2d;
using namespace cocosext;

namespace ___game_namespace___ {

////////////////////////////////////////////////////////
//

#define SLIDER_MARGIN 16.f

#define TOP_SHIFT 5.f
#define SHIFT_X 15.f
#define SHIFT_Y 15.f

//Tags
#define SLIDER_TAG_1_1   11
#define SLIDER_TAG_1_2   12
#define SLIDER_TAG_2_1   21
#define SLIDER_TAG_2_2   22

////////////////////////////////////////////////////////
//
/** DebugHUD
 * 
*/
////////////////////////////////////////////////////////

CCNode* DebugHUD::node(b2World* world, const cocos2d::CCSize& contentSize, const cocos2d::CCSize& parentSize)
{
	DebugHUD *pRet = new DebugHUD(world);
	CCAssert(pRet != NULL, NULL);
	if (pRet->init(contentSize, parentSize))
	{
		pRet->autorelease();
        return pRet;
	}
	else
	{
		CC_SAFE_DELETE(pRet)
		return NULL;
	}
}


DebugHUD::~DebugHUD()
{
	
}

static CCObject* addMenuButton(CCMenu* menu, const char *text, CCObject* target, SEL_MenuHandler selector)
{
    CCMenuItemFont::setFontSize(20);
    CCMenuItemFont::setFontName("Marker Felt");
    CCMenuItemFont* menuItem = CCMenuItemFont::itemFromString(text, target, selector);
    menuItem->setAnchorPoint(ccp(0.5f, 0.5f));
	menu->addChild(menuItem, 0);


	CCPoint pos(0.f, menu->getContentSize().height - menuItem->getContentSize().height/2.f - TOP_SHIFT);

    CCObject* child;
	CCARRAY_FOREACH(menu->getChildren(), child)
    {
		CCNode* node = (CCNode*)child;
        pos.x += node->getContentSize().width + SHIFT_X;
	}
	pos.x -= SHIFT_X;
	pos.x = (menu->getContentSize().width - pos.x)/2.f;
	CCARRAY_FOREACH(menu->getChildren(), child)
    {
		CCNode* node = (CCNode*)child;
		pos.x += node->getContentSize().width/2.f;
		node->setPosition(pos);
        pos.x += node->getContentSize().width/2.f + SHIFT_X;
	}
    
    return menuItem;
}

void createSliderModule(DebugHUD* hud, CCNode* parent, const char* key, float initValue, int tag)
{
	CEResetedSliderModule* sliderModule = CEResetedSliderModule::node(CCSprite::spriteWithFile(kSliderBk), 
                                            CCSprite::spriteWithFile(kSliderBkDisabled), 
                                            CCSprite::spriteWithFile(kSliderBtnNormal), 
                                            CCSprite::spriteWithFile(kSliderBtnDisabled), 
                                            SLIDER_MARGIN,
                                            key,
											initValue);
    sliderModule->setSliderTag(tag);
	sliderModule->registerDelegate(hud, true);
	parent->addChild(sliderModule);
}

bool DebugHUD::init(const cocos2d::CCSize& contentSize, const cocos2d::CCSize& parentSize)
{
    //self init
//    if (!CCNode::init() )
//        return false;

	this->setContentSize(contentSize);

	CCNode* layersLayer = CCNode::node();
	layersLayer->setContentSize(contentSize);
	layersLayer->setAnchorPoint(CCPointZero);
	this->addChild(layersLayer, 0);

	CCNode* sliderLayer = CCNode::node();
	sliderLayer->setContentSize(contentSize);
	sliderLayer->setAnchorPoint(CCPointZero);
	this->addChild(sliderLayer, 1);

	CCMenu* menu = CCMenu::node();
    menu->setAnchorPoint(CCPointZero);
    menu->setPosition(CCPointZero);
    menu->setContentSize(contentSize);
    this->addChild(menu, 3);

		//Debug draw
#if (USE_GLES_RENDERER == 1)
	_renderNode = CEGLESRenderNode::node(_world, PTM_RATIO);
	_renderNode->setPosition(ccp((contentSize.width - parentSize.width)/2.f, (contentSize.height - parentSize.height)/2.f));
	_renderNode->setIsVisible(false);
	layersLayer->addChild(_renderNode, 1);

	addMenuButton(menu, "Renderer", this, menu_selector(DebugHUD::onSwitchRenderer));
#endif

	CCNode* group = NULL;
	CCObject* caller = NULL;

//Slider group 1
	group = CESliderGroup::node();
	group->setContentSize(contentSize);
	group->setAnchorPoint(CCPointZero);
	group->setIsVisible(false);
	sliderLayer->addChild(group, 0);
	_groups.push_back(GroupForCaller(addMenuButton(menu, "Group 1", this, menu_selector(DebugHUD::onGroupsBtnClick)), group));

	createSliderModule(this, group, "gravity", _world->GetGravity().Length(), SLIDER_TAG_1_1);

//Slider group 2
	group = CESliderGroup::node();
	group->setContentSize(contentSize);
	group->setAnchorPoint(CCPointZero);
	group->setIsVisible(false);
	sliderLayer->addChild(group, 0);
	_groups.push_back(GroupForCaller(addMenuButton(menu, "Group 2", this, menu_selector(DebugHUD::onGroupsBtnClick)), group));

	createSliderModule(this, group, "slider_2_1", 0, SLIDER_TAG_2_1);
	createSliderModule(this, group, "slider_2_2", 0, SLIDER_TAG_2_2);

    return true;
}

////////////////////////////////////////////////////////
// CESliderDelegate interface

void DebugHUD::onValueChange(CESliderInterface* slider)
{
	switch (slider->getSliderTag())
	{
	case SLIDER_TAG_1_1:
		{
			b2Vec2 gravity(0.f, -1.f);
			gravity *= slider->getValue();
			_world->SetGravity(gravity);
			break;
		}
	case SLIDER_TAG_1_2: 
		{
			break;
		}
	case SLIDER_TAG_2_1: 
		{
			break;
		}
	case SLIDER_TAG_2_2: 
		{
			break;
		}
	default: break;
	}
}

////////////////////////////////////////////////////////
// private interface

void DebugHUD::onSwitchRenderer(CCObject* caller)
{
	_renderNode->setIsVisible(!_renderNode->getIsVisible());
}

void DebugHUD::onGroupsBtnClick(CCObject* caller)
{
	for (GroupForCallerList::const_iterator it = _groups.begin(); it != _groups.end(); ++it)
	{
		if (it->first == caller)
			it->second->setIsVisible(!it->second->getIsVisible());
		else
			it->second->setIsVisible(false);
	}
}

} // namespace ___game_namespace___
